<?php

define(C("ACCESSACTION"), true);
import('jinghuan.AdminAction');

/**
 * Class ApiAction
 */
class ArticleAction extends AdminAction
{
    var $upload_filter = array('gif', 'jpg', 'jpeg', 'png');

    function datatable()
    {
        $post = I('post.');
        $hash = array();
        $post["where"]['status'] = 1;
        $post["order"] = $post["order"] ? $post["order"] : 'id desc';
        $count = D(JH_ARTICLE)->where($post["where"])->count();
        $row = D(JH_ARTICLE)
            ->page($post["page"], $post["length"])
            ->where($post["where"])
            ->order($post["order"])
            ->select();


        $this->callback(array(
            'rows' => $row,
            'echo' => $post["echo"],
            'count' => $count
        ));
    }

    /**
     *
     */
    function edit()
    {
        $data = I('post.');
        $data = D(JH_ARTICLE)->find(intval($data['id']));
        $this->callback($data);
    }

    /**
     *
     */
    function do_edit()
    {
        $data = I('post.');
        D(JH_ARTICLE)->save($data);
        $data = D(JH_ARTICLE)->find($data['id']);
        $this->callback($data);
    }

    /**
     *
     */
    function do_add()
    {
        $post = I('post.');
        $post["u_time"] = $post["time"] = time();

        $result = D(JH_ARTICLE)->add($post);
        if ($result !== false) {
            $this->callback(true);
        }
    }


    function do_edit_content()
    {
        $post = I('post.');
        D(JH_ARTICLE)->save(array('id' => $post['id'], content => $post['content']));
        $result = D(JH_ARTICLE)->find($post['id']);
        $this->callback($result);
    }

    function do_delete()
    {
        $id = I('post.id');
        $row = D(JH_ARTICLE)
            ->where(array('id' => $id))
            ->save(['status' => 2]);
        if ($row === false) {
            $this->callback(false);
        } else {
            $this->callback(true);
        }

    }
}